package com.haierCamera.customapplication.di;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.CustomApplication;
import com.haierCamera.customapplication.api.ApiService;
import com.haierCamera.customapplication.utils.AppExecutors;
import com.haierCamera.customapplication.utils.LiveDataCallAdapterFactory;
import com.haierCamera.customapplication.utils.converter.GsonConverterFactory;
//import com.komlin.commonedition.Constants;
//import com.komlin.commonedition.api.ApiService;
//import com.komlin.commonedition.api.FaceApiService;
//import com.komlin.commonedition.utils.AppExecutors;
//import com.komlin.commonedition.utils.LiveDataCallAdapterFactory;
//import com.komlin.commonedition.utils.gson.converter.GsonConverterFactory;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import retrofit2.Retrofit;
import timber.log.Timber;

/**
 * 基础依赖提供
 *
 * @author yawei
 * @data on 2018/7/10 下午1:24
 * @email zyawei@live.com
 */
@Module()
public class AppModule {


    @Singleton
    @Provides
    Context provideApplication(CustomApplication app) {
        return app;
    }

    @Singleton
    @Provides
    AppExecutors appExecutors() {
        return new AppExecutors();
    }


    @Singleton
    @Provides
    OkHttpClient provideOkHttp(TokenManager tokenManager) {
        return new OkHttpClient.Builder()
                .connectTimeout(20000, TimeUnit.MILLISECONDS)
                .readTimeout(20000, TimeUnit.MILLISECONDS)
                .writeTimeout(20000, TimeUnit.MILLISECONDS)
                .addNetworkInterceptor(chain -> {

                    Request original = chain.request();
                    Request.Builder newBuilder = original.newBuilder();
                    String value = tokenManager.getToken();
//                    long schoolId = tokenManager.getSchoolId();
                    Log.i("TAG","Token-" + value);
                    if (!TextUtils.isEmpty(value)) {
                        newBuilder.addHeader("Token", value);
//                        newBuilder.addHeader("schoolId", String.valueOf(schoolId));
                    }
                    Log.i("TAG","Request URL=" +original.url() + "///token-" + value );
                    Timber.v("OKHttp Request URL= [%s]", original.url());
                    if (Objects.equals("POST", original.method())) {
                        RequestBody bodyUnSign = original.body();
                        assert bodyUnSign != null;
                        newBuilder.post(bodyUnSign);
                    } else if (Objects.equals("DELETE", original.method())) {
                        newBuilder.delete();
                    } else if (Objects.equals("PUT", original.method())) {
                        RequestBody bodyUnSign = original.body();
                        assert bodyUnSign != null;
                        newBuilder.put(bodyUnSign);
                    } else{
                        newBuilder.get();
                    }
                    Request request = newBuilder.build();
                    Response proceed = chain.proceed(request);
                    if (proceed.code()==20000){
                        //说明token过期

                    }
                    return proceed;
                })

                .build();
    }

    @Singleton
    @Provides
    ApiService provideApiService(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(new LiveDataCallAdapterFactory())
                .baseUrl(Constants.BASE_URL)
                .client(okHttpClient)
                .build()
                .create(ApiService.class);
    }

}
